Fix --release not compiling upstream deps with -O3
authorAlex Crichton <alex@alexcrichton.com>
Wed, 9 Jul 2014 20:55:00 +0000 (13:55 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 9 Jul 2014 20:55:00 +0000 (13:55 -0700)
This touches up the filtering logic to ensure that upstream dependencies as well
as local dependencies are built with optimizations when `cargo build --release`
is used.

src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_rustc.rs
tests/support/mod.rs
tests/test_cargo_compile.rs
tests/test_cargo_test.rs

index cadc97a9c444f8ded349cac2e576d7f5adf46d1b..e8f13b3a1b4bf76095e6f32fb1fae7a823ed8c95 100644 (file)
@@ -78,7 +78,7 @@ pub fn compile(manifest_path: &Path, options: CompileOptions) -> CargoResult<()>
     }).collect::<Vec<&Target>>();
 
     let mut config = try!(Config::new(shell, update, jobs));
-    try!(ops::compile_targets(targets.as_slice(), &package,
+    try!(ops::compile_targets(env.as_slice(), targets.as_slice(), &package,
          &PackageSet::new(packages.as_slice()), &mut config));
 
     Ok(())
index 3d3b5660e06137685d14a2cfb1fb9b5fe23679e4..14cf3d963ef06df1c34e0458a6a0f230d9537176 100644 (file)
@@ -41,7 +41,8 @@ fn uniq_target_dest<'a>(targets: &[&'a Target]) -> Option<&'a str> {
     curr.unwrap()
 }
 
-pub fn compile_targets<'a>(targets: &[&Target], pkg: &Package, deps: &PackageSet,
+pub fn compile_targets<'a>(env: &str, targets: &[&Target], pkg: &Package,
+                           deps: &PackageSet,
                            config: &'a mut Config<'a>) -> CargoResult<()> {
 
     if targets.is_empty() {
@@ -84,11 +85,10 @@ pub fn compile_targets<'a>(targets: &[&Target], pkg: &Package, deps: &PackageSet
     for dep in deps.iter() {
         // Only compile lib targets for dependencies
         let targets = dep.get_targets().iter().filter(|target| {
-            target.is_lib() && target.get_profile().is_compile()
+            target.is_lib() && target.get_profile().get_env() == env
         }).collect::<Vec<&Target>>();
 
-        jobs.push((dep,
-                   try!(compile(targets.as_slice(), dep, &mut cx))));
+        jobs.push((dep, try!(compile(targets.as_slice(), dep, &mut cx))));
     }
 
     cx.primary = true;
@@ -285,7 +285,7 @@ fn build_base_args(into: &mut Args,
             into.push(format!("metadata={}", m.metadata));
 
             into.push("-C".to_str());
-            into.push(format!("extra-filename={}", m.extra_filename));
+            into.push(format!("extra-filename=-{}", m.extra_filename));
         }
         None => {}
     }
index 9dfcd696ba9b24e26f206ff2ce60666a7c620a1f..72e643b6e6df28a32a7e50c96753d7f215e9aca7 100644 (file)
@@ -414,6 +414,7 @@ pub fn basic_bin_manifest(name: &str) -> String {
     "#, name, name)
 }
 
+pub static RUNNING:   &'static str = "     Running";
 pub static COMPILING: &'static str = "   Compiling";
 pub static FRESH:     &'static str = "       Fresh";
 pub static UPDATING:  &'static str = "    Updating";
index 0cc9214ee83d2f5148674ccdd2ee0ab0f93d0887..10f82fbb2fbff37aacbfc7dc58a289e8a2ec438f 100644 (file)
@@ -1,9 +1,10 @@
 use std::io::fs;
 use std::os;
 use std::path;
+use std::str;
 
 use support::{ResultTest, project, execs, main_file, escape_path, basic_bin_manifest};
-use support::COMPILING;
+use support::{COMPILING, RUNNING};
 use hamcrest::{assert_that, existing_file};
 use cargo;
 use cargo::util::{process, realpath};
@@ -954,3 +955,119 @@ test!(missing_lib_and_bin {
                        .with_stderr("either a [[lib]] or [[bin]] section \
                                      must be present\n"));
 })
+
+test!(verbose_build {
+    let mut p = project("foo");
+    p = p
+        .file("Cargo.toml", r#"
+            [package]
+
+            name = "test"
+            version = "0.0.0"
+            authors = []
+        "#)
+        .file("src/lib.rs", "");
+    let output = p.cargo_process("cargo-build").arg("-v")
+                  .exec_with_output().assert();
+    let out = str::from_utf8(output.output.as_slice()).assert();
+    let hash = out.slice_from(out.find_str("extra-filename=").unwrap() + 15);
+    let hash = hash.slice_to(17);
+    assert_eq!(out, format!("\
+{} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib -g \
+        -C metadata=test:-:0.0.0:-:file:{dir} \
+        -C extra-filename={hash} \
+        --out-dir {dir}{sep}target \
+        -L {dir}{sep}target \
+        -L {dir}{sep}target{sep}deps`
+{} test v0.0.0 (file:{dir})\n",
+                    RUNNING, COMPILING,
+                    dir = p.root().display(),
+                    sep = path::SEP,
+                    hash = hash).as_slice());
+})
+
+test!(verbose_release_build {
+    let mut p = project("foo");
+    p = p
+        .file("Cargo.toml", r#"
+            [package]
+
+            name = "test"
+            version = "0.0.0"
+            authors = []
+        "#)
+        .file("src/lib.rs", "");
+    let output = p.cargo_process("cargo-build").arg("-v").arg("--release")
+                  .exec_with_output().assert();
+    let out = str::from_utf8(output.output.as_slice()).assert();
+    let hash = out.slice_from(out.find_str("extra-filename=").unwrap() + 15);
+    let hash = hash.slice_to(17);
+    assert_eq!(out, format!("\
+{} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib \
+        --opt-level 3 \
+        -C metadata=test:-:0.0.0:-:file:{dir} \
+        -C extra-filename={hash} \
+        --out-dir {dir}{sep}target{sep}release \
+        -L {dir}{sep}target{sep}release \
+        -L {dir}{sep}target{sep}release{sep}deps`
+{} test v0.0.0 (file:{dir})\n",
+                    RUNNING, COMPILING,
+                    dir = p.root().display(),
+                    sep = path::SEP,
+                    hash = hash).as_slice());
+})
+
+test!(verbose_release_build_deps {
+    let mut p = project("foo");
+    p = p
+        .file("Cargo.toml", r#"
+            [package]
+
+            name = "test"
+            version = "0.0.0"
+            authors = []
+
+            [dependencies.foo]
+            path = "foo"
+        "#)
+        .file("src/lib.rs", "")
+        .file("foo/Cargo.toml", r#"
+            [package]
+
+            name = "foo"
+            version = "0.0.0"
+            authors = []
+        "#)
+        .file("foo/src/lib.rs", "");
+    let output = p.cargo_process("cargo-build").arg("-v").arg("--release")
+                  .exec_with_output().assert();
+    let out = str::from_utf8(output.output.as_slice()).assert();
+    let pos1 = out.find_str("extra-filename=").unwrap();
+    let hash1 = out.slice_from(pos1 + 15).slice_to(17);
+    let pos2 = out.slice_from(pos1 + 10).find_str("extra-filename=").unwrap();
+    let hash2 = out.slice_from(pos1 + 10 + pos2 + 15).slice_to(17);
+    assert_eq!(out, format!("\
+{running} `rustc {dir}{sep}foo{sep}src{sep}lib.rs --crate-name foo \
+        --crate-type lib \
+        --opt-level 3 \
+        -C metadata=foo:-:0.0.0:-:file:{dir} \
+        -C extra-filename={hash1} \
+        --out-dir {dir}{sep}target{sep}release{sep}deps \
+        -L {dir}{sep}target{sep}release{sep}deps \
+        -L {dir}{sep}target{sep}release{sep}deps`
+{running} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib \
+        --opt-level 3 \
+        -C metadata=test:-:0.0.0:-:file:{dir} \
+        -C extra-filename={hash2} \
+        --out-dir {dir}{sep}target{sep}release \
+        -L {dir}{sep}target{sep}release \
+        -L {dir}{sep}target{sep}release{sep}deps`
+{compiling} foo v0.0.0 (file:{dir})
+{compiling} test v0.0.0 (file:{dir})\n",
+                    running = RUNNING,
+                    compiling = COMPILING,
+                    dir = p.root().display(),
+                    sep = path::SEP,
+                    hash1 = hash1,
+                    hash2 = hash2).as_slice());
+})
index 62116160bb218ff7461192559a904e4eee7920aa..07a9f0e4f2fe921c1d7b59538ad9f1be91de1fe2 100644 (file)
@@ -55,3 +55,29 @@ test!(test_with_lib_dep {
 
     assert_that(p.cargo_process("cargo-test"), execs().with_status(0));
 })
+
+test!(test_with_deep_lib_dep {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "bar"
+            version = "0.0.1"
+            authors = []
+
+            [dependencies.foo]
+            path = "foo"
+        "#)
+        .file("src/lib.rs", "
+            extern crate foo;
+            pub fn bar() {}
+        ")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/lib.rs", "");
+
+    assert_that(p.cargo_process("cargo-test"), execs().with_status(0));
+})